$(function () {
	$("#code_type_add").on("click", function(){
		var treeObj = $.fn.zTree.getZTreeObj("codeTypeTree");
		//选中的节点
		var nodes = treeObj.getSelectedNodes();
		//判断参数当前有多少行
		if(nodes.length>0){
			var addTr = $("tr #addTr");
			if(addTr){
				$("#addTr").remove();
			}
			var trLength = $("#baseTypeTable tbody tr").length;
			trLength = trLength + 1;
			var treeNode = nodes[0];
			//左侧菜单树typeId
			var nodeId = treeNode.id;
			var nodePId = treeNode.pId;
			if(!nodePId){
				JY.Model.info("请选择一个二级项目!");
				return;
			}
			var html = "";
			html+="<tr id='addTr' style='background-color: #C0E2AA;'>";
			html+="<td class='center hidden-480'>"+(trLength)+"</td>";
   		 	html+="<td class='center'><input type='text' id='codeName' name='codeName' /></td>";
//   		 	html+="<td class='center'><input type='text' id='codeValue' name='codeValue' /></td>";
   		 	html+="<td class='center'><input type='text' id='sort' name='sort' /></td>";
   		 	html+="<td class='center'>";
   		 	html+="<a href='javascript: void(0);' onclick='saveBaseCodeValue("+nodeId+", this);'>保存</a>";
   		 	html+="&emsp;<a href='javascript: void(0);' onclick='updateCancel();'>取消</a>";
   		 	html+="</td>";
   		 	html+="</tr>";
   		 	$("#baseTypeTable tbody").append(html);
		}else{
			JY.Model.info("您没有选中任何二级节点!");
			return;
		}
	});
	
	$("#code_type_init").on("click", function(){
		JY.Model.confirm("您确定要进行初始化吗?", function(){
			var url = jypath+'/system/baseCodeValue/init';
			JY.Model.loading();
			JY.Ajax.doRequest(null, url, null, function(data){
				JY.Model.info(data.resMsg);
				loadCodeType();
			});
			JY.Model.loadingClose();
		});
	});
	
	loadCodeType();
});

function loadCodeType(){
	JY.Ajax.doRequest(
		null,
		jypath +'/system/baseCodeType/getBaseCodeTypeTree',
		{resourcesCode: resourcesCode, resourcesName: "resName_org_tree", projectId: projectId},
		function(data){
			//设置
			$.fn.zTree.init(
				$("#codeTypeTree"),
				{view:{selectedMulti:false,fontCss:{color:"#393939"}},
				data:{simpleData: {enable: true}},
				callback:{onClick:selectNode}},
				data.obj
			);
			var treeObj = $.fn.zTree.getZTreeObj("codeTypeTree");
		}
	);
}

/**
 * 选中节点
 * 非一级节点,查询code对应的值
 * @param event
 * @param treeId
 * @param treeNode
 */
function selectNode(event,treeId,treeNode) {
	var nodeId = treeNode.id;
	var nodePId = treeNode.pId;
	var nodeName = treeNode.name;
	if(nodePId){
		getCodeValueList(nodeId);
	}
}

function getCodeValueList(typeId){
	var params = {};
	params["projectId"] = projectId;
	params["resourcesCode"] = resourcesCode;
	params["resourcesName"] = "resName_org_tree";
	params["typeId"] = typeId;
	params["pageNum"] = 1;
	params["pageSize"] = 1000;
	JY.Ajax.doRequest(
		"baseOrgForm",
		jypath +'/system/baseCodeValue/list',
		params,
		function(data){
		     $("#baseTypeTable tbody").empty();
    		 //分页对象
        	 var page=data.obj;
        	 //数据集合
        	 var results=page.rows;
         	 var totalRecord=page.total;
        	 var html="";
    		 if(results!=null&&results.length>0){			
        		 for(var i = 0;i<results.length;i++){
            		 var result = results[i];
            		 html+="<tr>";
            		 html+="<td class='center hidden-480'>"+(i+1)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(result.codeName)+"</td>";
//            		 html+="<td class='center'>"+JY.Object.notEmpty(result.codeValue)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(result.sort)+"</td>";
            		 html+="<td class='center'>";
            		 if(result.codeName == '渠道转客' && result.remark == '初始化'){
					 }else{
                         html+="<a href='javascript: void(0);' onclick='updateRow(this, "+result.id+", "+result.typeId+");'>修改</a>";
					 }
            		 if(result.remark != "初始化"){
            			 html+="&nbsp;&nbsp;<a href='javascript: void(0);' onclick='delBaseCodeValue("+result.id+","+result.typeId+");'>删除</a>";
            		 }
            		 html+="</td>";
            		 html+="</tr>";
            	 } 
        		 $("#baseTypeTable tbody").append(html);
        		 
        	 }else{
        		html+="<tr><td colspan='5' class='center'>没有相关数据</td></tr>";
        		$("#baseTypeTable tbody").append(html);
        	 }	 	        	 
		}
	);
}

function saveBaseCodeValue(typeId){
	JY.Model.confirm("您确定要进行保存吗?", function(){
		var saveInputArr = $("#addTr input");
		var params = {};
		params["typeId"] = typeId;
		params["remark"] = "自定义";
		params["projectId"] = projectId;
		saveInputArr.each(function(){
			var inputName = $(this).attr("name");
			var inputVal = $(this).val();
			params[inputName] = inputVal;
			if("codeName" == inputName){
				//codeVal值于codeName一样.
				params["codeValue"] = inputVal;
			}
		});
		if(!params.codeName || !params.sort){
			JY.Model.info("参数和顺序不能为空!");
			return;
		}
		if(params.codeName.length > 10){
			JY.Model.info("参数最长长度为10!");
			return;
		}
		var url = jypath+'/system/baseCodeValue/add';
		JY.Ajax.doRequest(null, url, params, function(data){
			if(data.res == 0){
				JY.Model.info(data.resMsg);
			}else{
				JY.Model.info(data.resMsg, function(){
					getCodeValueList(typeId);
				});      				        		 
			}
		});
	});
}

function updateRow(obj, recordId, typeId){
	//当前点击事件a标签所在的行
	var selTr = $(obj).parent().parent();
	//于a标签同级的td元素
	var tdCols = $(obj).parent().siblings("td");
	var rowNum = "";
	var codeName = "";
	var sort = "";
	var codeValue="";
	for(var ii=0; ii<tdCols.length; ii++){
		var td = tdCols[ii];
		if(ii == 0){
			rowNum = $(td).text();
		}else if(ii == 1){
			codeName = $(td).text();
//		}else if(ii == 2){
//			codeValue = $(td).text();
		}else if(ii == 2){
			sort = $(td).text();
		}
	}
	
	var html = "";
	html+="<tr id='updateTr' style='background-color: #C0E2AA;'>";
	html+="<td class='center hidden-480'>"+rowNum+"</td>";
 	html+="<td class='center'><input type='text' id='codeName' name='codeName' value='"+codeName+"' /></td>";
// 	html+="<td class='center'><input type='text' id='codeValue' name='codeValue' value='"+codeValue+"' /></td>";
 	html+="<td class='center'><input type='text' id='sort' name='sort' value='"+sort+"' /></td>";
 	html+="<td class='center'>";
 	html+="<a href='javascript: void(0);' onclick='saveUpdateRow("+recordId+", "+typeId+", this);'>保存</a>";
 	html+="&emsp;<a href='javascript: void(0);' onclick='updateCancel();'>取消</a>";
 	html+="</td>";
 	html+="</tr>";
 	selTr.after(html);
 	selTr.remove();
}

function saveUpdateRow(recordId, typeId, obj){
	//当前点击事件a标签所在的行
	var selTr = $(obj).parent().parent();
	//于a标签同级的td元素
	var tdCols = $(obj).parent().siblings("td");
	var rowNum = "";
	var codeName = "";
	var codeValue="";
	var sort = "";
	for(var ii=0; ii<tdCols.length; ii++){
		var td = tdCols[ii];
		var inputVal = $(td).children().val();
		if(ii == 1){
			codeName = inputVal;
//		}else if(ii == 2){
//			codeValue = inputVal;
		}else if(ii == 2){
			sort = inputVal;
		}
		
	}
	if(!codeName || !sort){
		JY.Model.info("参数和顺序不能为空!");
		return;
	}
	if(codeName.length > 10){
		JY.Model.info("参数最长长度为10!");
		return;
	}
	var params = {};
	params["id"] = recordId;
	params["typeId"] = typeId;
	params["codeName"] = codeName;
	params["codeValue"] = codeValue;
	params["sort"] = sort;
	params["projectId"] = projectId;
	
	var url = jypath+'/system/baseCodeValue/edit';
	JY.Ajax.doRequest(null, url, params, function(data){
		if(data.res == 0){
			JY.Model.info(data.resMsg);
		}else{
			JY.Model.info(data.resMsg, function(){
	    		var treeObj = $.fn.zTree.getZTreeObj("codeTypeTree");
	    		//选中的节点
	    		var nodes = treeObj.getSelectedNodes();
	    		var treeNode = nodes[0];
	    		var nodeId = treeNode.id;
	    		getCodeValueList(nodeId);
	    	});
		}
	});
}

function updateCancel(){
	var treeObj = $.fn.zTree.getZTreeObj("codeTypeTree");
	//选中的节点
	var nodes = treeObj.getSelectedNodes();
	var treeNode = nodes[0];
	var nodeId = treeNode.id;
	getCodeValueList(nodeId);
}

function delBaseCodeValue(codeValueId, typeId){
	JY.Model.confirm("您确定要删除吗?", function(){
		var url = jypath+'/system/baseCodeValue/delete';
		var params = {};
		params["id"] = codeValueId;
		JY.Ajax.doRequest(null, url, params, function(data){
	    	JY.Model.info(data.resMsg, function(){
	    		getCodeValueList(typeId);
	    	});      				        		 
		});
	});
}
